

import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:flutter/services.dart';
import 'package:pointycastle/asymmetric/api.dart';

// RSA 加解密工具类
class EncryptUntils{
    //分段 加密
    static Future<String> encodeString(String content) async{
      var publicKeyStr = await rootBundle.loadString('enpty/rsa/publish.pem');
      var publicKey = RSAKeyParser().parse(publicKeyStr) as RSAPublicKey;
      final encrypter = Encrypter(RSA(publicKey: publicKey));
      List<int> sourceBytes = utf8.encode(content);
      int inputLen = sourceBytes.length;
      int maxLen = 117;
      List<int> totalBytes = [];
      for (var i = 0; i < inputLen; i += maxLen) {
        int endLen = inputLen - i;
        List<int> item;
        if (endLen > maxLen) {
          item = sourceBytes.sublist(i, i + maxLen);
        }
        else {
          item = sourceBytes.sublist(i, i + endLen);
        }
        totalBytes.addAll(encrypter.encryptBytes(item).bytes);
      }
      return base64.encode(totalBytes);
  }
}